package com.taru.order.controller;


import com.taru.order.common.Data;
import com.taru.order.common.JsonResult;
import com.taru.order.entity.Articl;
import com.taru.order.entity.Category;
import com.taru.order.service.CateServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CateGoryController {

    CateServiceImpl cateService=new CateServiceImpl();

    /**
     * 获取分类
     * @return
     */
    @RequestMapping(value = "/categories",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCategories(){
        JsonResult result=null;
        try{
            List<Category> categoryList=cateService.getCategories();
            if(categoryList !=null){
                result =new JsonResult(200,"获取成功",categoryList);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 添加分类
     */
    @RequestMapping(value = "/categories",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addCategory(@RequestBody Category category){
        JsonResult result=null;
        try{
            int code=cateService.addCategory(category);
            if(code>0){
                result =new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改分类
     */
    @RequestMapping(value = "/categories",method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult modCategory(@RequestBody Category category){
        JsonResult result=null;
        try{
            int code=cateService.modCategory(category);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 批量删除分类
     */
    @RequestMapping(value = "/categories",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult removeCategories(@RequestBody() List<Integer> list){
        JsonResult result=null;
        try{
            int code=cateService.remove(list);
            if(code>0){
                result =new JsonResult(200,"删除成功");
            }else {
                result=new JsonResult(404,"删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 按分类查文章列表（已发布）
     */
    @RequestMapping(value = "/categories/articl",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listArticls( String cateId,
                                  @RequestParam(value ="pageNum",required = false,defaultValue = "1") Integer pageNum,
                                  @RequestParam(value ="pageSize",required = false,defaultValue = "10") Integer pageSize){
        JsonResult result=null;
        try{
            Data data=cateService.listArticls(pageNum,pageSize,cateId);
            if(data!=null){
                result =new JsonResult(200,"获取成功",data);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 按分类查询最新的一篇文章
     */
    @RequestMapping(value = "/categories/articl/{cateId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listArticlOne(@PathVariable("cateId") String cateId){
        JsonResult result=null;
        try{
            Articl articl=cateService.listArticlOne(cateId);
            if(articl!=null){
                result =new JsonResult(200,"获取成功",articl);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 按分类查询月度热榜
     */
    @RequestMapping(value = "/categories/month/{cateId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listArticlsByMonth(@PathVariable("cateId") String cateId,String year,String month){
        JsonResult result=null;
        try{
            List list=cateService.listArticlsByMonth(cateId,year,month);
            if(list!=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 按分类查询年度热榜
     */
    @RequestMapping(value = "/categories/year/{cateId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listArticlsByMonth(@PathVariable("cateId") String cateId,String year){
        JsonResult result=null;
        try{
            List list=cateService.listArticlsByYear(cateId,year);
            if(list!=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 分页获取所有最新文章
     */
    @RequestMapping(value = "/categories/allArticl/{cateId}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult removeCategories(@PathVariable("cateId") String cateId,Integer pageNum,Integer pageSize ){
        JsonResult result=null;
        try{
            Data data=cateService.listAllArticls(cateId,pageNum,pageSize);
            if(data!=null){
                result =new JsonResult(200,"获取成功",data);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
}
